﻿
Module Carrier
    Private _CONNECTION_STRING(2) As String
    Private _CS_ACC2003 As New csfnet2.Access2003
    Private _CS_MYSQL As New csfnet2.MySQL
    Friend _FILE_CONF As String

    Friend _CONN_ACC2003 As New OleDb.OleDbConnection
    Friend _CMD_ACC2003 As New OleDb.OleDbCommand
    Friend _DR_ACC2003 As OleDb.OleDbDataReader

    Friend _CONN_MYSQL As New MySql.Data.MySqlClient.MySqlConnection
    Friend _CMD_MYSQL As New MySql.Data.MySqlClient.MySqlCommand
    Friend _DR_MYSQL As MySql.Data.MySqlClient.MySqlDataReader

    Private _SQLCOMMANDGET As New cmd.GetDataStatic
    Private _SQLCOMMANDPUSH As New cmd.PushData

    Friend Sub SET_ConfigurationFile()
        ISCONTAINERRORS = True
        _FILE_CONF = Application.StartupPath & "\Files\conf.mdb"
        _CONNECTION_STRING(0) = _CS_ACC2003.Microsoft_OLEDB_Standard(_FILE_CONF, "Admin", "")
    End Sub

    Friend Sub GET_ConfigurationFile(ByVal i As Integer)
        Try
            '_CONN_ACC2003.Close()
            _CONN_ACC2003 = New OleDb.OleDbConnection(_CONNECTION_STRING(i))
            _CONN_ACC2003.Open()
        Catch ErrHandle As OleDb.OleDbException
            MessageBox.Show("Tidak dapat membuka berkas konfigurasi." & vbCrLf & vbCrLf & "Keterangan:" & vbCrLf & ErrHandle.Message, "Kesalahan", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Friend Sub GET_ConfigurationFile_Value()
        Call GET_ConfigurationFile(0)

        Try
            _CMD_ACC2003.Connection = _CONN_ACC2003
            _CMD_ACC2003.CommandType = CommandType.Text
            _CMD_ACC2003.CommandText = _SQLCOMMANDGET.FileConfiguration

            _CMD_ACC2003 = New OleDb.OleDbCommand(_SQLCOMMANDGET.FileConfiguration, _CONN_ACC2003)
            _DR_ACC2003 = _CMD_ACC2003.ExecuteReader

            If _DR_ACC2003.HasRows Then
                _DR_ACC2003.Read()
            End If

            FILECONFIG.ServerAddress = _DR_ACC2003.GetString(0)
            FILECONFIG.Port = _DR_ACC2003.GetValue(1)
            FILECONFIG.Database = _DR_ACC2003.GetString(2)
            FILECONFIG.Username = _DR_ACC2003.GetString(3)
            FILECONFIG.Password = _DR_ACC2003.GetString(4)

            _DR_ACC2003.Close()

            Call PUSH_FileConfig_Disposed()

        Catch ErrHandleOledb As OleDb.OleDbException
            Call ShowError(False, , ErrHandleOledb.ErrorCode, , ErrHandleOledb.Message, )
            Exit Sub
        Catch ErrHandleData As DataException
            Call ShowError(False, , , , ErrHandleData.Message, )
            Exit Sub
        Catch ErrHandleApplication As ApplicationException
            Call ShowError(False, , , , ErrHandleApplication.Message, )
            Exit Sub
        Catch ErrHandleOther As Exception
            Call ShowError(False, , , , ErrHandleOther.Message, )
            Exit Sub
        End Try
    End Sub

    Friend Sub PUSH_ConnectingToServer(ByVal ServerAddress As String, ByVal Port As Integer, ByVal Database As String, ByVal Username As String, ByVal Password As String)
        Mainframe.Cursor = Cursors.AppStarting
        _CONNECTION_STRING(1) = _CS_MYSQL.MySQL_NET_SpecifyingPort(ServerAddress, Port, Username, Password, Database)

        Try
            '_CONN_MYSQL.Close()
            _CONN_MYSQL = New MySql.Data.MySqlClient.MySqlConnection(_CONNECTION_STRING(1))
            _CONN_MYSQL.Open()
            _CONN_MYSQL.Close()
            _CONN_MYSQL.Dispose()
            Mainframe.Cursor = Cursors.Default
            ISCONTAINERRORS = False
            Call ShowError(True, "Berhasil Terhubung", , , , )
        Catch ErrHandleMySQL As MySql.Data.MySqlClient.MySqlException
            Call ShowError(False, "Gagal Melakukan Sambungan", ErrHandleMySQL.ErrorCode, ErrHandleMySQL.Number, ErrHandleMySQL.Message, )
        Catch ErrHandleOther As Exception
            Call ShowError(False, "Gagal Melakukan Sambungan", , , ErrHandleOther.Message, )
        End Try

    End Sub

    Friend Sub ShowError(ByVal IsNoError As Boolean, Optional ByVal HeaderPesan As String = "", Optional ByVal ErrorCode As String = "", Optional ByVal ErrorNumber As String = "", Optional ByVal ErrorMessage As String = "", Optional ByVal ErrorException As String = "")
        Dim LOC As Int16

        If IsNoError = True Then
            With Mainframe
                .txtErrorMessage.Clear()
                .txtErrorMessage.Text = HeaderPesan
                .txtErrorMessage.Text += vbCrLf & "--- Konfigurasi ---"
                .txtErrorMessage.Text += vbCrLf & "Alamat Server: " & FILECONFIG.ServerAddress
                .txtErrorMessage.Text += vbCrLf & "Port: " & FILECONFIG.Port
                .txtErrorMessage.Text += vbCrLf & "Database: " & FILECONFIG.Database
                .txtErrorMessage.Text += vbCrLf & "Username: " & FILECONFIG.Username
                .txtErrorMessage.Text += vbCrLf & "Password: ***********"

                If .txtErrorMessage.Text.Contains("Berhasil Terhubung") Then
                    LOC = .txtErrorMessage.Find("Berhasil Terhubung")
                    .txtErrorMessage.Select(LOC, 18)
                    .txtErrorMessage.SelectionColor = Color.Green
                End If
            End With
        Else
            ISCONTAINERRORS = True
            With Mainframe
                .txtErrorMessage.Clear()
                .txtErrorMessage.Text = HeaderPesan
                .txtErrorMessage.Text += vbCrLf & "--- Konfigurasi ---"
                .txtErrorMessage.Text += vbCrLf & "Alamat Server: " & FILECONFIG.ServerAddress
                .txtErrorMessage.Text += vbCrLf & "Port: " & FILECONFIG.Port
                .txtErrorMessage.Text += vbCrLf & "Database: " & FILECONFIG.Database
                .txtErrorMessage.Text += vbCrLf & "Username: " & FILECONFIG.Username
                If Not FILECONFIG.Password = "" Then
                    .txtErrorMessage.Text += vbCrLf & "Password: ***********" & vbCrLf
                Else
                    .txtErrorMessage.Text += vbCrLf & "Password: " & vbCrLf
                End If
                .txtErrorMessage.Text += vbCrLf & "--- Pesan Kesalahan ---"
                .txtErrorMessage.Text += vbCrLf & "Kode Error: " & ErrorCode
                .txtErrorMessage.Text += vbCrLf & "Nomor Error: " & ErrorNumber
                .txtErrorMessage.Text += vbCrLf & "Pesan Error: " & ErrorMessage
                '.txtErrorMessage.Text += vbCrLf & "Exception: " & ErrorException

                If .txtErrorMessage.Text.Contains("--- Pesan Kesalahan ---") Then
                    LOC = .txtErrorMessage.Find("--- Pesan Kesalahan ---")
                    .txtErrorMessage.Select(LOC, 23)
                    .txtErrorMessage.SelectionColor = Color.Red
                End If

                If .txtErrorMessage.Text.Contains("Gagal Melakukan Sambungan") Then
                    LOC = .txtErrorMessage.Find("Gagal Melakukan Sambungan")
                    .txtErrorMessage.Select(LOC, 25)
                    .txtErrorMessage.SelectionColor = Color.Red
                End If
            End With
        End If
    End Sub

    Public Sub PUSH_Config_to_Database(ByVal ServerAddress As String, ByVal Port As Integer, ByVal Username As String, ByVal Password As String, ByVal DatabaseName As String)
        If ISCONTAINERRORS = False Then

            Call GET_ConfigurationFile(0)

            SSQL = "UPDATE dbconfig SET dbconfig.server = '" & ServerAddress & "', dbconfig.port = " & Port & ", dbconfig.database = '" & DatabaseName & "', dbconfig.uid = '" & Username & "', dbconfig.password = '" & Password & "' WHERE ((dbconfig.iddbconfig)=1);"

            Try
                _CMD_ACC2003 = New OleDb.OleDbCommand
                _CMD_ACC2003.Connection = _CONN_ACC2003
                _CMD_ACC2003.CommandType = CommandType.Text
                _CMD_ACC2003.CommandText = SSQL
                _CMD_ACC2003.ExecuteNonQuery()

                INFO = MessageBox.Show("Pengaturan baru berhasil disimpan", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information)

                ISCONTAINERRORS = True

            Catch ErrHandle As DataException
                INFO = MessageBox.Show(ErrHandle.Message, "Kesalahan", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Catch ErrHandle As InsufficientMemoryException
                INFO = MessageBox.Show(ErrHandle.Message, "Kesalahan", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Catch ErrHandle As Exception
                INFO = MessageBox.Show(ErrHandle.Message, "Kesalahan", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub

    Friend Sub PUSH_FileConfig_Disposed()
        _CONN_ACC2003.Close()
        _CONN_ACC2003.Dispose()
    End Sub

    Friend Sub PUSH_Dispose_Active_Connection()
        _CONN_MYSQL.Close()
        _CONN_MYSQL.Dispose()
    End Sub
End Module
